<template>
  <div v-loading="pageLoading">
    <z-form :data="snData" :formProps="formProps" :fields="fields"></z-form>
  </div>
</template>

<script>
import commonMixin from '@/views/_common/mixin'

export default {
  mixins: [commonMixin],
  props: {
    sn: {
      type: String,
      default: '',
    },
  },
  watch: {
    sn: {
      handler() {
        this.refreshData()
      },
    },
  },
  data() {
    return {
      query: {
        sn: null,
      },
      snData: {},
      formProps: {
        labelWidth: '140px',
        disabled: true,
      },
      fields: [
        { title: '产品SN', key: 'serial_number', span: 8 },
        { title: '出货序号', key: 'shipping_sn', span: 8 },
        { title: '包装状态', key: 'current_status', span: 8 },
        { title: '序号产品总数', key: 'sn_batch_qty', span: 8 },
        { title: '当前OK数量', key: 'ok_qty', span: 8 },
        { title: '当前NG数量', key: 'bad_qty', span: 8 },
        { title: '所属工单', key: 'mo_no', span: 8 },
        { title: '拼板状态', key: 'bind_status_txt', span: 16 },
        { title: '拦截状态', key: 'hold_flag_txt', span: 8 },
        { title: '拦截原因', key: 'hold_reason', span: 16 },
        { title: '指站状态', key: 'next_station_txt', span: 8 },
        { title: '指站原因', key: 'next_reason', span: 16 },
      ],
    }
  },
  methods: {
    refreshData() {
      if (this.sn) {
        this.query.sn = this.sn
        this.getInfo()
      } else {
        this.query.sn = null
        this.snData = {}
      }
    },
    getInfo() {
      this.pageLoading = true
      this.$request(this.$api.wip + 'getsnwip', {
        sn: this.query.sn,
      })
        .then((v) => {
          if (v && v.length > 0) {
            let tmp = {}
            if (v.length > 1)
              tmp = v.find(
                (x) =>
                  x.serial_number.toLowerCase() == this.query.sn.toLowerCase() ||
                  x.shipping_sn.toLowerCase() == this.query.sn.toLowerCase(),
              )
            else tmp = v[0]
            tmp.current_status = tmp.sn_status == 1 ? '未包装' : '已包装'
            tmp.hold_flag_txt = tmp.hold_flag == 1 ? '拦截中' : ''
            tmp.next_station_txt = !!tmp.next_station ? '指站中' : ''
            tmp.bind_status_txt = { 1: '已拼板', 2: '已分板' }[tmp.bind_status] || ''
            this.snData = tmp
            this.$emit('after-getsn', tmp)
          } else {
            this.currentMo = {}
            this.boundList = []
            this.$message.error('没有找到产品序号信息')
            this.$emit('after-getsn', {})
          }
        })
        .finally(() => {
          this.pageLoading = false
        })
    },
  },
  created: function () {
    this.refreshData()
  },
}
</script>

<style lang="scss" scoped>
</style>